﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VirtualTerminalControl
{
    public class MyVScrollBar : VScrollBar
    {

        public MyVScrollBar()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.AllPaintingInWmPaint
                      , true);
            //    SetStyle(ControlStyles.OptimizedDoubleBuffer |
            //ControlStyles.AllPaintingInWmPaint  ,
            //     true);
            this.Minimum = 0;
            this.Maximum = 1;
        }
        ///// <summary>
        ///// 设置最大值
        ///// </summary>
        ///// <param name="Max"></param>
        //public void SetMax(int Max)
        //{
        //    this.Maximum = Max;
        //    //this.BeginInvoke(new Action(() =>
        //    //{
        //    //    this.Maximum = Max;
        //    //}));
        //}


        ///// <summary>
        ///// 自动增加
        ///// </summary>
        //public void Max_auto_increment()
        //{
        //    this.Maximum++;
        //}

        /// <summary>
        /// 安全更新值
        /// </summary>
        /// <param name="value"></param>
        public void SetValue(int value)
        {

            //if (value > Maximum)
            //{
            //    if (value > Minimum)
            //    {
            //        this.Maximum = value + this.LargeChange+1;
            //        this.Value = value;
            //    }
            //    else
            //    {
            //        this.Value = Minimum;
            //    }

            //}
            //else if (value>Minimum)
            //{

            //}
            this.Value = value;
            //   this.Value = value > Maximum ? Maximum : (value >= Minimum ? value : Minimum);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            // e.Handled = true;
            base.OnKeyUp(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            // e.Handled = true;
            base.OnKeyDown(e);

        }
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            // e.Handled = true;
            base.OnKeyPress(e);

        }
        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {

            base.OnPreviewKeyDown(e);

        }
        protected override void OnLostFocus(EventArgs e)
        {

            //  Console.WriteLine("OnLostFocus");
            base.OnLostFocus(e);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            // Console.WriteLine("OnGotFocus");
            base.OnGotFocus(e);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle bounds = this.ClientRectangle;

            // 绘制滚动条背景
            g.FillRectangle(Brushes.LightGray, bounds);

            // 计算滑块的位置和大小
            int sliderHeight = (int)((double)bounds.Height * ((double)(this.LargeChange) / (double)(this.Maximum - this.Minimum + this.LargeChange)));
            int sliderTop = (int)((double)(bounds.Height - sliderHeight) * ((double)(this.Value - this.Minimum) / (double)(this.Maximum - this.Minimum)));
            Rectangle sliderRect = new Rectangle(bounds.X, bounds.Y + sliderTop, bounds.Width, sliderHeight);

            // 绘制滑块
            g.FillRectangle(Brushes.DarkGray, sliderRect);

            // 绘制滑块边框
            g.DrawRectangle(Pens.Black, sliderRect);
        }
    }
}
